<?php
require_once('./admin/connexion.php');
require ('./func.req.php');
if ($install) {
    ?>
    <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
    <div id="box" onclick="document.getElementById('box').style.display='none';" style="margin:20%;" class="error-box rounded" title="Fermer la fen&ecirc;tre">
        L'installation n'a pas encore &eacute;t&eacute; r&eacute;alis&eacute;e. <a href="./install/index.php">Cliquez ici</a> pour commencer.
    </div>
    <?php
} else {
    require('./config/tbl.php');
    if ($line_info['maintenance_choix_info'] == "Oui") {
        show_maintenance();
    } else {
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title><?php echo $site; ?> | Inscription</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <meta http-equiv="Content-Style-Type" content="text/css" />
                <meta http-equiv="content-language" content="fr,en" />
                <link rel='shortcut icon' type="image/x-icon" href='./images/favicon.ico' />
                <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/register.css" title="" media="all" />
                <link rel="stylesheet" type="text/css" href="./css/news.css" title="" media="all" />
                <script type="text/javascript" src="./js/news.js"></script>
            </head>
            <body>
                <div id="block">
                    <div id="contentblock">
                        <div class="wrapper">
                            <?php
                            show_header();
                            show_menu();
                            show_newsbox();
                            show_userbox();
                            $num1 = rand(0, 9);
                            $num2 = rand(0, 9);
                            ?>
                            <div class="mainbox">
                                <h2>Inscription</h2>
                                <br />
                                <?php
                                $test_login = @$_POST['login'];
                                //Selection de la table
                                $query_user = "SELECT * FROM " . $database . "." . $tb_utilisateurs . " WHERE login = '$test_login' ";

                                //Affichage de la selection
                                $result_user = @mysql_query($query_user) OR DIE("Erreur de la s&eacute;lection des utilisateurs : " . @mysql_error());
                                //Verification du pseudo
                                $testpseudo = @mysql_num_rows($result_user);
//                            $row = mysql_fetch_array($result_user);
//                            echo $row[0];
                                if (isset($_POST['register'])) { // on verifie la presence des variables de formulaire (si le formulaire a �t� envoy�)
                                    if (($_POST['login'] == "") || ($_POST['pass'] == "") || ($_POST['email'] == "") || ($_POST['prenom'] == "") || ($_POST['nom'] == "") || ($_POST['sexe'] == "--") || ($_POST['reponse'] == "") || ($_POST['captcha'] == "")) { // si un champ n'est pas specifie >> message d'erreur
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            Echec d'enregistrement. Tout les champs ne sont pas remplis.
                                        </div>
                                        <br />
                                        <?php
                                    } else if ($testpseudo != 0) {
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            Echec d'enregistrement. Ce pseudo est d&eacute;j&agrave; utilis&eacute;.
                                        </div>
                                        <br />
                                        <?php
                                    } else if ($_POST['login'] == $_POST['pass']) {
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            Echec d'enregistrement. Le pseudo et le mot de passe ne peuvent &ecirc;tre identiques.
                                        </div>
                                        <br />
                                        <?php
                                    } else if ($_POST['captcha'] != $_POST['captcha_rep']) {
                                        ?>
                                        <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                            Echec d'enregistrement. Le Captcha entr&eacute; est incorrect.
                                        </div>
                                        <br />
                                        <?php
                                    } else {
                                        if ($_POST['pass'] == $_POST['pass2']) { // on verifie si le mot de passe et le mot de passe confirme ont la meme valeur
                                            $_POST['login'] = htmlspecialchars($_POST['login']); // On rend inoffensives les balises HTML que le visiteur a pu rentrer
                                            $_POST['nom'] = htmlspecialchars($_POST['nom']);
                                            $_POST['prenom'] = htmlspecialchars($_POST['prenom']);
                                            $_POST['reponse'] = htmlspecialchars($_POST['reponse']);
                                            if (preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email'])) {
// on passe toutes les variables $POST en variables
                                                $login = mysql_escape_string($_POST['login']);
                                                $pass = md5($_POST['pass']); // ici, on crypte le mot de passe a l'aide de MD5
                                                $nom = mysql_escape_string($_POST['nom']);
                                                $prenom = mysql_escape_string($_POST['prenom']);
                                                $email = mysql_escape_string($_POST['email']);
                                                $date_inscription = date('Y') . "-" . date('m') . "-" . date('j');
                                                $date_naissance = $_POST['annee'] . "-" . $_POST['mois'] . "-" . $_POST['jour'];
                                                $sexe = $_POST['sexe'];
                                                $question = mysql_escape_string($_POST['question']);
                                                $reponse = utf8_encode($_POST['reponse']);

                                                // on fait l'INSERT dans la base de donnees
                                                $add_user = @sprintf("INSERT INTO " . $database . "." . $tb_utilisateurs . " (login, pass, nom, prenom, email, sexe, privilege, question_secrete, reponse_secrete, date_naissance, date_inscription) VALUES ('$login', '$pass', '$nom', '$prenom', '$email', '$sexe', 'user', '$question', '$reponse', '$date_naissance', '$date_inscription')");
                                                @mysql_select_db($database, $dbconnect);
                                                $result = @mysql_query($add_user, $dbconnect) or die(@mysql_error());
                                                ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                                    Enregistrement r&eacute;ussi. Vous pouvez d&egrave;s maintenant <a href="./login.php" title="Connectez-vous !"><b><u>vous connectez</u></b></a>.
                                                </div>
                                                <br />
                                                <?php
                                            } else {
                                                ?>
                                                <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                                    L'email entr&eacute; est incorrect. V&eacute;rifiez que vous n'avez pas fait d'erreur.
                                                </div>
                                                <br />
                                                <?php
                                            }
                                        } else {
                                            ?>
                                            <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                                Echec d'enregistrement. Tout les champs ne sont pas remplis.
                                            </div>
                                            <br />
                                            <?php
                                        }
                                    }
                                }
                                ?>
                                <center>
                                    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                                        <table border="0" cellspacing="0">
                                            <tr>
                                                <td colspan="3" class="title_form">Vos informations</td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Nom : </td>
                                                <td><input type="text" name="nom" size="30" value="<?php echo @$_POST['nom']; ?>" class="border" /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Pr&eacute;nom : </td>
                                                <td><input type="text" name="prenom" size="30" value="<?php echo @$_POST['prenom']; ?>" class="border" /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Adresse e-mail : </td>
                                                <td><input type="text" name="email" size="30" value="<?php echo @$_POST['email']; ?>" class="border" /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Date de naissance : </td>
                                                <td>
                                                    <?php
                                                    echo "\n\t\t<select name='jour'>";
                                                    for ($jour = 1; $jour <= 31; $jour++) {
                                                        if ($jour < 10) {
                                                            echo "\n\t\t\t<OPTION value=$jour name='jour_debut'\">0$jour</OPTION>";
                                                        } else {
                                                            echo "\n\t\t\t<OPTION value=$jour name='jour_debut'\">$jour</OPTION>";
                                                        }
                                                    }
                                                    echo "\n\t\t</select>";
                                                    echo "\n\t\t<select name='mois'>";
                                                    for ($mois = 1; $mois <= 12; $mois++) {
                                                        if ($mois < 10) {
                                                            echo "\n\t\t\t<OPTION value=$mois name='mois_debut'\">0$mois</OPTION>";
                                                        } else {
                                                            echo "\n\t\t\t<OPTION value=$mois name='mois_debut'\">$mois</OPTION>";
                                                        }
                                                    }
                                                    echo "\n\t\t</select>";
                                                    echo "\n\t\t<select name='annee'>";
                                                    for ($annee = 1950; $annee <= date('Y'); $annee++) {
                                                        echo "\n\t\t\t<OPTION value=$annee name='annee_debut'>$annee</OPTION>";
                                                    }
                                                    echo "\n\t\t</select>";
                                                    ?>
                                                    <i class="px10">(Jour / Mois / Ann&eacute;e)</i>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Sexe : </td>
                                                <td>
                                                    <select name="sexe">
                                                        <option>--</option>
                                                        <option>Homme</option>
                                                        <option>Femme</option>
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="title_form">Votre profil</td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Pseudo : </td>
                                                <td><input type="text" name="login" size="30" value="<?php echo @$_POST['login']; ?>" class="border" title="Attention ! Ceci sera votre login." /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Mot de passe :</td>
                                                <td><input type="password" name="pass" size="30" value="" class="border" /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">R&eacute;p&eacute;tez le mot de passe : </td>
                                                <td><input type="password" name="pass2" size="30" value="" class="border" /></td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Question secr&egrave;te : </td>
                                                <td>
                                                    <select name="question">
                                                        <option><?php echo $question_secrete1; ?></option>
                                                        <option><?php echo $question_secrete2; ?></option>
                                                        <option><?php echo $question_secrete3; ?></option>
                                                        <option><?php echo $question_secrete4; ?></option>
                                                        <option><?php echo $question_secrete5; ?></option>
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">R&eacute;ponse secr&egrave;te : </td>
                                                <td><input type="text" name="reponse" size="30" value="<?php echo @$_POST['reponse']; ?>" class="border" /> <i class="px10">(En minuscule)</i></td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="title_form">Test</td>
                                            </tr>
                                            <tr>
                                                <td class="text_form">Captcha : </td>
                                                <td><?php echo $num1 . " + " . $num2; ?> = <input type="text" name="captcha" class="border"/><input type="hidden" name="captcha_rep" size="30" value="<?php echo $num1 + $num2; ?>" /></td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" valign="middle" align="center"><input type="submit" name="register" value="S'inscrire" class="btn" /></td>
                                            </tr>
                                        </table>
                                        <br />
                                    </form>
                                </center>
                                <br />
                            </div>
                            <?php show_footer(); ?>
                        </div>
                    </div>
                </div>
            </body>
        </html>
        <?php
    }
}
?>